<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div>
    <h1>核算检测系统</h1>
    <form method="get" action="/hesuan/list">
        <select name="id" id="selectHospital">
            <option value="">-请选择机构-</option>
        </select>
        <input type="submit" value="提交">
    </form>

    <a href="/hesuan/toAdd">添加</a>

    <table border="1px" cellpadding="0" cellspacing="0">
        <tr>
            <td>被检测人</td>
            <td>被检测人手机号</td>
            <td>被检测人身份证号</td>
            <td>检测机构</td>
            <td>检测日期</td>
            <td>检测结果</td>
            <td>操作</td>
        </tr>


        <tr th:each="l : ${list}" th:bgcolor="${l.assayResult == 1 ? 'red':''}">
            <td th:text="${l.assayUser}"></td>
            <td th:text="${l.phone}"></td>
            <td th:text="${l.cardNum}"></td>
            <td th:text="${l.hospital_name}"></td>
            <td th:text="${#dates.format(l.assayTime,'yyyy-MM-dd')}"></td>
            <td th:text="${l.assayResult == 1?'确诊':'检测中'}"></td>
            <td>
                <input type="hidden" th:value="${l.id}" class="cid">
                <a href="javascript:void(0)" onclick="up($(this))">确诊</a>
            </td>
        </tr>


    </table>
    
</div>

<script src="/webjars/jquery/3.4.1/jquery.js"></script>

<script th:inline="javascript">

    function up(_this){
        if(confirm("请确定该检测者是否确诊？")){
                $.ajax({
                    url: "/hesuan/"+_this.prev().val(),
                    method:'post',
                    success:function (data){
                        if(data > 0){
                            window.location.href="/hesuan/list"
                        }
                    }
                })
        }else{
            alert("已取消该操作！");
        }
    }
    $(function (){

        //隔行变色 偶数行
        $("tr:even").each(function (){
            if(!$(this).attr("bgcolor")){
                $(this).attr("bgcolor","pink");
            }
        })

        //第一行要深色
        $("tr:first").attr("bgcolor","blue")

        var id = [[${id}]]
        //请求后台拿到数据——机构的数据
        $.ajax({
            url:'/hospital',
            type:'get',
            dataType:'json',
            success:function (data){
                console.log(data);
                let optionStr = "<option value=\"\">-请选择机构-</option>";
                for (let i = 0; i < data.length; i++) {

                    if(id == data[i].id){
                        optionStr += "<option selected value=\""+data[i].id+"\">"+data[i].name+"</option>"
                    }else{
                        optionStr += "<option value=\""+data[i].id+"\">"+data[i].name+"</option>"
                    }
                }
                $("#selectHospital").html(optionStr);
            }
        })
    })
</script>

</body>
</html>